<template>
  <div style="width: 100%;" class="card-area">
    <layout-content topTitle="查询" bottomTitle="帖子列表">
      <!-- 搜索区域 -->
      <template v-slot:topCard>
        <search-view ref="searchView" @search="search"/>
      </template>
      <!-- 表格区域 -->
      <template v-slot:bottomCard>
        <a-table
          ref="TableInfo"
          :columns="columns"
          rowKey="id"
          :dataSource="dataSource"
          :loading="loading"
          @change="handleTableChange"
          :pagination="pagination"
        >
          <template slot="area" slot-scope="text, record">
            <span>{{ record.province }}{{ record.city }}</span>
          </template>
          <template slot="isRecycle" slot-scope="text, record">
            <span :class="text == '1' ? 'blue-text' : 'gray-text'" @click="recycleChangeClick(record)">{{ isRecycleMap[text] }}</span>
          </template>
          <template slot="isRecommend" slot-scope="text, record">
            <span :class="text == '1' ? 'blue-text' : 'gray-text'" @click="recommendChangeClick(record)">{{ isRecommendMap[text] }}</span>
          </template>
          <template slot="operation" slot-scope="text, record">
            <a-button type="link" @click="info(record)" class="icon">详情</a-button>
            <a-button type="link" @click="comment(record)" class="icon">评论</a-button>
            <a-button type="link" @click="onDelete(record)" class="icon">删除</a-button>
          </template>
        </a-table>
        <!-- 查看 -->
        <info-record
          :visible="infoVisible"
          @close="infoVisible = false"
          :initRecord="selectedRecord"
          :apiURL="apiURL"
        >
        </info-record>
        <!-- 是否推荐 -->
        <status-record
          ref="exactStatus"
          :visible="statusVisible"
          :initRecord="selectedRecord"
          @close="statusVisible = false"
          @success="onStatusSuccess"
          :apiURL="apiURL"
          :tag="statusTag"
        >
        </status-record>
      </template>
    </layout-content>
  </div>
</template>

<script>
import columns from './columns';
import modal from '~/tool/modal';
import apiURL from './requestURL';
import infoRecord from './info';
import layoutContent from '@/components/tool/layout-content';
import searchView from './search-view';
import statusRecord from './status';

export default {
  name: 'mainPage',
  components: {layoutContent, modal, searchView, infoRecord, statusRecord},
  data() {
    return {
      // 添加或编辑
      // addOrEdit: 0,
      // 组件渲染的key
      // addKey: new Date().getTime(),
      // 表头
      columns,
      // 接口url
      apiURL,
      // 组件的可见性
      // addVisible: false,
      infoVisible: false,
      statusVisible: false,
      statusTag: -1,
      // 当前选中的记录
      selectedRecord: {},

      dataSource: [],
      sortedInfo: null,
      paginationInfo: null,
      pagination: {
        size: 'small',
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false,
      isRecycleMap: Object.freeze({
        '0': '否',
        '1': '是'
      }),
      isRecommendMap: Object.freeze({
        '0': '否',
        '1': '是'
      }),
    };
  },
  computed: {},
  created() {
  },
  mounted() {
    this.search();
  },
  methods: {
    recycleChangeClick(record) {
      let that = this;
      this.selectedRecord = record;
      that.statusTag = 0;
      record.status = record.isRecycle == '1' ? 1 : 0;
      this.statusVisible = true;
    },
    recommendChangeClick(record) {
      let that = this;
      this.selectedRecord = record;
      that.statusTag = 1;
      record.status = record.isRecommend == '1' ? 1 : 0;
      this.statusVisible = true;
    },
    // 修改状态成功回调
    onStatusSuccess(msg = '修改成功') {
      this.statusVisible = false;
      this.$message.success(msg);
      this.search(this.params);
    },
    // handleOperationClick(key, record = {}) {
    //   this.addOrEdit = key === 'add' ? 0 : 1;
    //   this.selectedRecord = record;
    //   this[`addKey`]++;
    //   this[`addVisible`] = true;
    // },
    /**
     * @desc: 新增成功时的回调函数，隐藏新增组件，提示，查询
     * @param {type}
     * @author: youzi
     */
    // onAddSuccess() {
    //   this.addVisible = false;
    //   this.$message.success('操作成功');
    //   this.search();
    //   // this.$refs['searchView'].handleSearch();
    // },
    /**
     * @desc: 查看组件，传入一条记录
     * @param {object} record 当前选择的这条记录
     * @author: youzi
     */
    info(record) {
      record.imgs = record.imgs && record.imgs.length > 0 ? record.imgs.split(',') : [];
      record.video = record.video && record.video.length > 0 ? record.video : '';
      this.selectedRecord = record;
      this.infoVisible = true;
      console.log(record);
    },
    comment(record) {
      this.$router.push({
        path: '/articlecomment',
        query: {
          articleId: record.id
        }
      });
    },
    /**
     * @desc: 删除记录
     * @param {object} 一条记录
     * @author: youzi
     */
    onDelete(record) {
      let that = this;
      this.$confirm({
        title: '确定删除吗?',
        content: '删除将不能查看哦！',
        centered: true,
        onOk() {
          that.$post(apiURL.deleteRecord, {id: record.id}).then(() => {
            that.$message.success('删除成功');
            that.search();
          });
        },
        onCancel() {
        }
      });
    },
    /**
     * @desc: 分页信息改变时的回调函数
     * @param 分页信息，过滤信息，排序信息
     * @author: youzi
     */
    handleTableChange(pagination, filters, sorter) {
      // 将这两个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination;
      this.sortedInfo = sorter;
      /* this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order
      }); */
      // this.$refs['searchView'].handleSearch();
      this.search();
    },
    /**
     * @desc: 调用fetch的入口函数，先拿到当前列的排序和过滤信息
     * @author: youzi
     */
    search(params = {}) {
      let {sortedInfo} = this;
      let sortField, sortOrder;
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field;
        sortOrder = sortedInfo.order;
      }
      this.fetch({
        ...params,
        sortField: sortField,
        sortOrder: sortOrder
      });
    },
    /**
     * @desc: 查询记录的函数
     * @param {object} params 包含查询条件，分页等信息
     * @author: youzi
     */
    fetch(params = {}) {
      this.loading = true;
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current;
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize;
        params.count = this.paginationInfo.pageSize;
        params.currentPage = this.paginationInfo.current;
      } else {
        // 如果分页信息为空，则设置为默认值
        params.count = this.pagination.defaultPageSize;
        params.currentPage = this.pagination.defaultCurrent;
      }
      this.$post(apiURL.getRecords, {
        ...params
      })
        .then(r => {
          let data = r.data.data;
          let pagination = {...this.pagination};
          pagination.total = data.total;
          this.dataSource = data.rows;
          this.pagination = pagination;
          this.loading = false;
        })
        .catch(e => {
          console.error(e);
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
/* @import '../../../../static/less/Common';
.card-area-bottom {
  margin-top: 1rem;
}
.card-area {
  .card-area-top,
  .card-area-bottom {
    border-radius: 6px;
  }
  .card-title {
    font-size: 1rem !important;
    font-weight: bold;
    color: #393d47;
    border-left: 4px solid #408bf4;
    padding-left: 15px;
  }
} */
.code-img {
  width: 4rem !important;
}

.reject-reason-content {
  text-align: center;
}

.divider {
  margin: 0 4px;
}

.desc {
  font-size: 20px !important;
  color: rgba(0, 0, 0, 0.6);
}

.icon {
  /* svg {
    height: 20px;
    width: 20px;
  } */
  padding: 0;
  border: none;
  color: #1890ff;
}

.card-area {
  .extra-btn {
    width: 86px;
  }
}

.search-btn {
  margin-top: 8px;
}

.border-span {
  width: 2px;
  /* border: 1px solid rgb(207, 215, 222); */
  background-color: #cfd7de;
  height: 16px;
  margin-right: 2px;
  display: inline-block;
}
.blue-text {
  text-decoration: underline;
  color: #2eabff;
  cursor: pointer;
}
.gray-text {
  text-decoration: underline;
  color: #666666;
  cursor: pointer;
}
</style>
